/*
	序列号列表
		序列号管理
		序列号修改
		序列号添加
*/

//登录信息
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');
var DICT_AGT = JSON.parse(sessionStorage.getItem('kplus_web_dict_agt'));
var DICT_ADMIN = JSON.parse(sessionStorage.getItem('kplus_web_dict_admin'));
var DICT_PNAME  = JSON.parse(sessionStorage.getItem('kplus_web_dict_pName'));


//获取字典 存储字典
if(!Cookies.getJSON('kplus_web_dict')){ getAllDict(); }
//dom 
$(function(){

	layui.use(['layer', 'form', 'table'], function(){
		var layer = layui.layer,
			form = layui.form,
			table = layui.table;

		//ajax data	
		var ajaxData = {
			aId: adminLoginedInfo.aId,
			aToken: adminLoginedInfo.aToken
		};
		//获取经销商列表
		createDicts(DICT_AGT, DICT_PNAME);
		form.render();
		//获取序列号
		function getSerialNumbers(ajaxData){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			$.ajax({
				url: BASE_SERVICEURL + URlS.psn,
				type:'GET',
				data: ajaxData,
				success: function(responseData){
					if(responseData.msg.code == 200){
						layer.close(loadIndex);
						//console.log(responseData);
						createTable(createTableData(responseData));
					}else{
						layer.close(loadIndex);
						layer.msg(responseData.msg.msg, {time: 2000});
					}
				},
				error: function(responseData){
					layer.close(loadIndex);
					layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
						//跳转500
						location.href = './500.html';
					}})
				}
			});
		};
		getSerialNumbers(ajaxData);
		//构建表格数据
		function createTableData(responseData){
			//console.log(ALL_DICTS)
			//console.log(responseData)
			var data = responseData.data.data;
			$('#dataSize').text(data.length);
			var data_arr = [];
			for (var i = data.length - 1; i >= 0; i--) {
				var _item = {
					psn: data[i].code,
					agent: DICT_AGT[data[i].agent],
					product: DICT_PNAME[data[i].belong],
					admin: DICT_ADMIN[data[i].create_adm_id],
					create_time: formatTime(data[i].format_time),
					use_time: data[i].used_time,
					active_time: data[i].active_time,
					status: data[i].status
				};
				data_arr.push(_item);
			}
			return data_arr;
		}
		//构建表格
		function createTable(tableData){
			table.render({
				elem: '#tableContent',
				limit: 30, //分页
				height: 680,
				page: true,
				cols:[[ //表头
					{field: 'psn', title: '序列号', sort: true, fixed: 'left', width:120},
					{field: 'agent', title: '渠道代理',width:110, sort: true},
					{field: 'product', title: '产品', sort: true} ,
					{field: 'admin', title: '创建者', width:110},
					{field: 'create_time', title: '生成时间', width:80},
					{field: 'use_time', title: '使用时间', sort: true},
					{field: 'active_time', title: '激活时间',width:110},
					{field: 'status', title: '状态', width:80,sort: true},
					{field: 'option', title: '操作', toolbar:'#toolbar'}
				]],
				data:tableData
			});
		}
		//综合检索
		form.on('submit(search)', function(data){
			getSerialNumbers($.extend({},data.field, ajaxData));
			return false;
		})
		//详情查看
		table.on('tool(serial-number-list-table)', function(obj){
			var data = obj.data;   //本行数据
			var layEvent = obj.event; 
			var tr = obj.tr;  //当前行
			//console.log(data)
			if(layEvent == 'seeDetatil'){  //查看详情
				x_admin_show('查看详情', 'serial-number-detail.html?psn='+data.psn+'', 1200, 800);
			};
		});
	})

})
